﻿Imports System.Data.OracleClient
Imports WindowsApplication1.funciones1
'Imports System.Data
Imports Microsoft.Data.Odbc
Public Class DETALLE_PEDIDO
    Dim conexion As String = "Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes;Pwd=redes;"
    Dim conexion2 As String = "Server=xe;Uid=redes;Pwd=redes;"

    Dim querysecuencia As String = "select max(ID_ORVEN)+ 1 as id_orven from TB_VNT_ORDEN_VENTA"
    Dim camposecuencia As String = "ID_ORVEN"
    Dim temp As String = traersecuencia(querysecuencia, camposecuencia)

    Dim coso As String
    Dim VectorDeDatos(4) As TextBox
    Public Function LlenarVector() As TextBox()
        VectorDeDatos(0) = txt_idpedido2
        VectorDeDatos(1) = txt_id_producto
        VectorDeDatos(2) = TXT_CANTIDAD
        VectorDeDatos(3) = TXT_PRECIO
        VectorDeDatos(4) = TXT_SUBTOTAL

        Return VectorDeDatos
    End Function
    Private Sub DETALLE_PEDIDO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
        Dim combo() As String = {"en proceso", "pendiente", "entregado"}
        cmb_estado.Items.AddRange(combo)

        txt_idPedidoE.Text = temp
        VectorDeDatos = LlenarVector()

        BarraNavegacion1.Inicializar("Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes;Pwd=redes;", "TB_VNT_DETTAB_OV", VectorDeDatos)
        txt_idpedido2.Text = txt_id_pedido.Text

        Dim oraCon As New OracleConnection(conexion2)
        oraCon.Open()
        Dim oraCommand As OracleCommand = New OracleCommand
        oraCommand.Connection = oraCon
        oraCommand.CommandText = "SELECT ID_PRODUCTO, NOMBRE from tb_ina_producto"
        Dim oraDataAdapter As New OracleDataAdapter
        oraDataAdapter.SelectCommand = oraCommand

        Dim tbProducto As New DataTable
        oraDataAdapter.Fill(tbProducto)


        CMB_PRODUCTO.DataSource = tbProducto
        CMB_PRODUCTO.ValueMember = "id_producto"
        CMB_PRODUCTO.DisplayMember = "nombre"

           End Sub

    Private Sub CMB_PRODUCTO_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CMB_PRODUCTO.TextChanged
        If CMB_PRODUCTO.ValueMember.Any Then
            txt_id_producto.Text = CMB_PRODUCTO.SelectedValue
        End If
    End Sub

    Private Sub TXT_CANTIDAD_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TXT_CANTIDAD.EnabledChanged

        CMB_PRODUCTO.Enabled = TXT_CANTIDAD.Enabled.ToString
        dgv_pedido.Enabled = TXT_CANTIDAD.Enabled.ToString
        cmb_estado.Enabled = TXT_CANTIDAD.Enabled.ToString
        TXT_FECHA.Enabled = TXT_CANTIDAD.Enabled.ToString
        txt_idPedidoE.Enabled = TXT_CANTIDAD.Enabled.ToString
        cmb_precio.Enabled = TXT_CANTIDAD.Enabled.ToString
        cmb_volumen.Enabled = TXT_CANTIDAD.Enabled.ToString
        cmb_total.Enabled = TXT_CANTIDAD.Enabled.ToString

    End Sub

    Private Sub txt_idPedidoE_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_idPedidoE.EnabledChanged
        If txt_idPedidoE.Enabled = True Then
            txt_idPedidoE.Enabled = False
            cmb_precio.Enabled = False
            TXT_SUBTOTAL.Enabled = False
            cmb_volumen.Enabled = False
        End If
        If TXT_FECHA.Text = "" Then
            TXT_FECHA.Text = Date.Today.ToShortDateString
        End If
    End Sub
    Private Sub txt_idPedidoE_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_idPedidoE.TextChanged

        If txt_idPedidoE.Text = "" Then
            txt_idPedidoE.Text = temp
        End If

        txt_idpedido2.Text = txt_idPedidoE.Text
        coso = txt_idPedidoE.Text
    End Sub

    Private Sub cmb_estado_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_estado.TextChanged
        txt_estado.Text = cmb_estado.SelectedItem
    End Sub

   
    Private Sub btn_guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_guardar.Click

        Dim oConexion As OdbcConnection
        oConexion = New OdbcConnection(conexion)
        Dim queryString As String = "INSERT INTO TB_VNT_ORDEN_VENTA VALUES " & _
        " ('" & Me.txt_idPedidoE.Text & "','" & Me.TXT_FECHA.Text & "','" & Me.txt_estado.Text & "')"
        Dim command As New OdbcCommand(queryString)
        command.Connection = oConexion
        oConexion.Open()
        command.ExecuteNonQuery()

        oConexion.Close()
        MessageBox.Show("Validacion exitosa.", "Operacion Exitosa", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub TXT_CANTIDAD_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXT_CANTIDAD.TextChanged
        txt_idpedido2.Text = coso
        Try
            Dim cantidad As Decimal = TXT_CANTIDAD.Text
            Dim precio As Decimal = TXT_PRECIO.Text
            Dim multi As Decimal = cantidad * precio

            Dim pago As Decimal = (multi)

            TXT_SUBTOTAL.Text = multi
         
        Catch ex As Exception
            'MessageBox.Show(ex.ToString, "Error")
        End Try
      

    End Sub

    Private Sub txt_id_producto_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_id_producto.TextChanged
        Try
            Dim querycombo2 As String = "SELECT PRECIO FROM TB_INA_PRODUCTO WHERE ID_PRODUCTO=" & Me.txt_id_producto.Text & ""
            Dim valuecombo2 As String = "precio" 'valor a insertar en tabla de acuerdo al combo
            Dim displaycombo2 As String = "precio" 'valor a mostrar en el combo

            Dim querycombo3 As String = "SELECT VOLUMEN FROM TB_INA_PRODUCTO WHERE ID_PRODUCTO=" & Me.txt_id_producto.Text & ""
            Dim valuecombo3 As String = "volumen" 'valor a insertar en tabla de acuerdo al combo
            Dim displaycombo3 As String = "volumen" 'valor a mostrar en el combo

            llenarcombo(cmb_precio, querycombo2, valuecombo2, displaycombo2)
            llenarcombo(cmb_volumen, querycombo3, valuecombo3, displaycombo3)
        Catch ex As Exception
            'MessageBox.Show(ex.ToString, "Error")
        End Try
        
    End Sub

    Private Sub cmb_precio_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_precio.EnabledChanged
        If cmb_precio.Enabled = True Then

            cmb_precio.Enabled = False

        End If
    End Sub

    Private Sub cmb_precio_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_precio.TextChanged
        If cmb_precio.ValueMember.Any Then
            TXT_PRECIO.Text = cmb_precio.SelectedValue
        End If
    End Sub

    Private Sub cmb_volumen_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_volumen.EnabledChanged
        If cmb_volumen.Enabled = True Then

            cmb_volumen.Enabled = False
        End If
    End Sub

    Private Sub cmb_volumen_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_volumen.TextChanged
        If (cmb_volumen.Text = "0") Then
            MessageBox.Show("No hay existencias")
        End If
    End Sub

    Private Sub TXT_SUBTOTAL_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TXT_SUBTOTAL.EnabledChanged
        If TXT_SUBTOTAL.Enabled = True Then

            TXT_SUBTOTAL.Enabled = False

        End If
    End Sub

    Private Sub btn_actualiza_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_actualiza.Click
        Dim querydgv As String = " select b.id_orven,a.nombre,b.cantidad,b.precio,b.subtotal from tb_ina_producto a, tb_vnt_dettab_ov b   where a.id_producto = b.id_producto And id_orven = " & Me.txt_idPedidoE.Text & ""
        dgv_pedido.DataSource = LlenarDgv(querydgv)

        If TXT_CANTIDAD.Text = "" Then
            Dim querycombo4 As String = "SELECT SUM(SUBTOTAL) AS SUBTOTAL FROM TB_VNT_DETTAB_OV WHERE ID_ORVEN=" & Me.txt_idPedidoE.Text & ""
            Dim valuecombo4 As String = "SUBTOTAL" 'valor a insertar en tabla de acuerdo al combo
            Dim displaycombo4 As String = "SUBTOTAL" 'valor a mostrar en el combo
            llenarcombo(cmb_total, querycombo4, valuecombo4, displaycombo4)

        End If

    End Sub

  
    Private Sub cmb_total_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_total.EnabledChanged
        If cmb_total.Enabled = True Then

            cmb_total.Enabled = False
        End If
    End Sub
End Class



